import React from 'react';
import { connect } from 'dva';
import _ from 'lodash';
import { InputEditor, TextWithTags } from '@/components/BaseTable';

@connect(state => ({}))
export default class AveragePriceCellRenderer extends React.PureComponent {
  state = {};

  changeRow = value => {
    let cloneValue = value;
    // const { isBatchAdjust } = this.props;
    let rowData = _.cloneDeep(this.props.rowData);
    const rowDatas = [];

    // // 普调版本规则---begin
    // if (isBatchAdjust) {
    //   // 初始值
    //   let averagePriceInit = rowData.averagePrice_house_init * 1;
    //   if (rowData.isParking) {
    //     averagePriceInit = rowData.averagePrice_parking_init * 1;
    //   }
    //   // 计算上下限 20%
    //   const v = averagePriceInit + averagePriceInit * Math.abs(0.2);

    //   if (value > v) {
    //     cloneValue = v;
    //   }
    // }
    // // 普调版本规则---end

    if (rowData.children && rowData.children.length > 0) {
      rowData.children.forEach(element => {
        if (element.canEditPrice) {
          if (element.isParking === 1) {
            rowDatas.push({ ...element, averagePrice_parking: cloneValue });
          } else {
            rowDatas.push({ ...element, averagePrice_house: cloneValue });
          }
        }
      });
    }
    if (rowData.isParking === 1) {
      rowData = {
        ...rowData,
        averagePrice: cloneValue,
        averagePrice_parking: cloneValue,
        children: [],
      };
    } else {
      rowData = {
        ...rowData,
        averagePrice: cloneValue,
        averagePrice_house: cloneValue,
        children: [],
      };
    }

    rowData = {
      ...rowData,
      averagePrice: cloneValue,
      children: [],
    };
    rowDatas.push(rowData);
    const { dispatch } = this.props;
    dispatch({
      type: 'PriceManageItems/saveRows',
      payload: {
        rowData: rowDatas,
        category: this.props.category,
        isMarketing: this.props.isMarketing,
      },
    });
  };

  render() {
    const { editing, rowData, cellData } = this.props;

    // 是否可以编辑均价需要特殊处理问题
    //  canEditPrice 代表是否可被改变均价
    //  是否出现编辑均价按钮 应该是
    //    1、站规版 org_build节点 canEditPrice===true
    //    2、非站规版 canEditPrice===true
    let option = false;

    if (this.props.category === 'strategy') {
      if (rowData.type === 'org_build' || rowData.isParkingParent || rowData.isParkingLeaf) {
        option = rowData.canEditPrice;
      }
    } else {
      option = rowData.canEditPrice;
    }
    // {editing && option ? (

    return (
      <>
        {(editing && rowData.canEditPrice && rowData.needEditPrice) ||
        (editing && rowData.name === '其它' && rowData.needEditPrice) ? (
          <InputEditor
            precision={2}
            validateStatus={rowData.errorPrice ? 'error' : 'success'}
            onChange={this.changeRow}
            {...this.props}
            cellData={cellData.number}
          />
        ) : (
          <TextWithTags {...this.props} />
        )}
      </>
    );
  }
}
